package com.zrx.springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrx.springboot.entity.Cart;
import com.zrx.springboot.entity.CartVo;
import com.zrx.springboot.entity.UserCartVo;
import com.zrx.springboot.mapper.CartMapper;
import com.zrx.springboot.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private CartMapper cartMapper;
    /**
     * 根据用户查询购物车
     */
   @Override
   public List<CartVo> findCartListByUser(Integer userId){
        return cartMapper.findCartListByUserId(userId);
   }
    /**
     * 统计当前用户购物车的总计
     */
    @Override
    public double getCartItemTotal(List<CartVo> list){
        double sum = 0.0;
        for (CartVo cart: list) {
            sum += cart.getCount() * cart.getNewPrice();
        }
        return sum;
    }
    /**
     * 批量删除
     */
    @Override
    public String darchDelete(String ids){
        if (ids != null){
          String[] idArray = ids.split(",");
          cartMapper.deleteBatchIds(Arrays.asList(idArray));
        }
        return "success";
    }

    /**
     * 包装用户购物车信息
     */
    @Override
    public UserCartVo wrapperCart(List<CartVo> list) {
        //将用户的购物车信息放在session
        UserCartVo userCartVo = new UserCartVo();
        userCartVo.setNum(list.size());
        userCartVo.setTotalPrice(getCartItemTotal(list));
        return userCartVo;
    }
    /**
     * 根据购物车id查询对象记录
     */
    @Override
    public List<CartVo> fianCartByIdss(String ids){
        System.out.println(Arrays.asList(ids));
        String[] idArray = ids.split(",");
        return cartMapper.findCartListByIds(Arrays.asList(idArray));
    }
}
